package com.ojoin.trade.supplyloan_core.web.dao.impl;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlProcessInfoDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlProcessInfo;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlProcessInfoVO;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 16-4-6
 * Time: 下午3:44
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbSlProcessInfoDaoImpl extends BaseDaoImpl<TbSlProcessInfo> implements TbSlProcessInfoDao {
    @Override
    public List<TbSlProcessInfoVO> queryNotTbSlProcessInfo() throws BusinessException {
        String mapMethodName = "selectnotprocess";
        try {
            List list = this.sqlSessionTemplate.selectList(
                    getSqlName(mapMethodName));
            if (list != null && list.size() > 0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("查询未启动流程的贷款数据表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;
    }

    @Override
    public void updateTbSlProcessInfo(Map<String,Object> map) throws BusinessException {
        String mapMethodName="update";
        try {
          this.sqlSessionTemplate.update(
                  getSqlName(mapMethodName),map);
        } catch (Exception e) {
            throw new DaoException(String.format("更新流程的贷款数据表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbSlProcessInfoVO> queryTbSlProcessInfoByJoinid(Pageable page,Integer user_id_of_join_of_debtor) throws BusinessException {
        String mapMethodName = "selectByJoinid";
        try {
            List list = this.sqlSessionTemplate.selectList(
                    getSqlName(mapMethodName),user_id_of_join_of_debtor,this.getPageBounds(page));
            if (list != null && list.size() > 0)
                return list;
        } catch (Exception e) {
            throw new DaoException(String.format("根据join用户查询流程的贷款数据表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;
    }

    @Override
        public void lockById(int id) throws BusinessException {
            String mapMethodName = "lockid";

            try {
                this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),
                        id);
            } catch (Exception e) {
                throw new DaoException(String.format("加锁出错->" + id + "出错！语句：%s",
                        getSqlName(mapMethodName)), e);
            }

        }

    @Override
    public TbSlProcessInfoVO queryLoanInfoVoByProcess(Integer id) throws BusinessException {
        String mapMethodName = "selectLoaninfoByprocess";

        try {
          return   this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),
                    id);
        } catch (Exception e) {
            throw new DaoException(String.format("根据流程id查询实时账户信息出错->" + id + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
}

    @Override
    public void cancelCredit() throws BusinessException {
        String mapMethodName ="cancel";
        String curdate= TimeUtils.getCurDate();
        try{
            this.sqlSessionTemplate.update(getSqlName(mapMethodName),curdate);
        }catch (Exception e) {
            throw new DaoException(String.format("流程表的自动取消出错！语句：%s",
                    getSqlName(mapMethodName)),e);
        }
    }

    @Override
    public TbSlProcessInfoVO queryMobileByProcessid(Integer processinfo) throws BusinessException {
        String mapMethodName = "selectmobilebyprocessid";

        try {
            return   this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),
                    processinfo);
        } catch (Exception e) {
            throw new DaoException(String.format("根据流程id查询手机号码信息出错->" + processinfo + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }
}
